package com.animations;

import com.beans.BirdBean;
import com.beans.ScoresBean;
import creator.ui.controller.VoicePlayer;
import creator.ui.graphics.Magicanvas;
import creator.ui.graphics.polaroid.Animation;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.PrintStream;
import java.util.ArrayList;

public class ScoresAction extends Animation {

  private ScoresBean scores;
  private ArrayList<Image> scoresList;
  private static int myScores = 0;
  private static int Scores;

  public void loadBean() {
    this.scores = new ScoresBean();
    getBeans().add(this.scores);
  }

  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    if (this.scoresList == null) this.scoresList = this.scores.getScores();

    if (myScores <= 9) {
      g2d.drawImage(
        (Image) this.scoresList.get(myScores % 10),
        ScoresBean.getScoresX(),
        ScoresBean.getScoresY(),
        getMagicanvas()
      );
    } else if (myScores <= 99) {
      g2d.drawImage(
        (Image) this.scoresList.get(myScores / 10 % 10),
        ScoresBean.getScores2X(),
        ScoresBean.getScoresY(),
        getMagicanvas()
      );

      g2d.drawImage(
        (Image) this.scoresList.get(myScores % 10),
        ScoresBean.getScores2X() + ScoresBean.getScoresW(),
        ScoresBean.getScoresY(),
        getMagicanvas()
      );
    } else if (myScores <= 999) {
      g2d.drawImage(
        (Image) this.scoresList.get(myScores / 100 % 10),
        ScoresBean.getScores3X(),
        ScoresBean.getScoresY(),
        getMagicanvas()
      );
      g2d.drawImage(
        (Image) this.scoresList.get(myScores / 10 % 10),
        ScoresBean.getScores3X() + ScoresBean.getScoresW(),
        ScoresBean.getScoresY(),
        getMagicanvas()
      );
      g2d.drawImage(
        (Image) this.scoresList.get(myScores % 10),
        ScoresBean.getScores3X() + ScoresBean.getScoresW() * 2,
        ScoresBean.getScoresY(),
        getMagicanvas()
      );
    }
  }

  public void run() {
    myScores = 0;
    int i = 0;
    while (BirdBean._isAlive()) {
      System.out.println("-> Pipes size : " + PipeAction.pipes.size());
      if (PipeAction.pipes.size() > 0) {
        System.out.println(
          "-> Pipe x :" +
          ((Pipes) PipeAction.pipes.get(i)).getPipeX() +
          "\n-> Scores : " +
          myScores
        );
        if (((Pipes) PipeAction.pipes.get(i)).getPipeX() < 88) {
          i++;
          VoicePlayer.play("/res/sounds/sfx_point.wav");
          myScores += 1;
          getMagicanvas().repaint();
        }
      }
      try {
        Thread.sleep(300L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    Scores = myScores;
    myScores = 1000;
    getMagicanvas().repaint();
  }

  public static int getMyScores() {
    return Scores;
  }
}
